package com.atguigu.spzx.user.controller;

import com.atguigu.spzx.common.handler.interceptor.SpzxServiceAuthInterceptor;
import com.atguigu.spzx.model.entity.h5.Region;
import com.atguigu.spzx.model.entity.h5.UserAddress;
import com.atguigu.spzx.model.result.Result;
import com.atguigu.spzx.user.service.RegionService;
import com.atguigu.spzx.user.service.UserAddressService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/user/userAddress")
@Tag(name = "用户地址管理模块")
public class UserAddressController {
    @Resource
    UserAddressService userAddressService;
    @Resource
    RegionService regionService;
    @Operation(summary = "新增用户地址")
    @PostMapping("auth/save")
    public Result save(@RequestBody UserAddress userAddress){
        //获取新增地址的用户id
        Long id = SpzxServiceAuthInterceptor.THREAD_LOCAL.get().getId();
        //新增地址如果为默认地址 需要更新其他的默认地址为非默认
        if(userAddress.getIsDefault()==1){
            userAddressService.update(Wrappers.lambdaUpdate(UserAddress.class)
                    .set(UserAddress::getIsDefault , 0 )
                    .eq(UserAddress::getIsDefault , 1)
                    .eq(UserAddress::getUserId , id));//更新当前用户的地址为非默认
        }
        //新增的地址可以选择是否为默认
        //1、查询省市区 code对应的 区域名称
        Region privinceRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getProvinceCode())
                .select(Region::getName));
        Region cityRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getCityCode())
                .select(Region::getName));
        Region districtRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getDistrictCode())
                .select(Region::getName));
        String fullAddr = privinceRegion.getName()+cityRegion.getName()
                +districtRegion.getName()+" "
                + userAddress.getAddress();
        userAddress.setFullAddress(fullAddr);
        userAddress.setUserId(id);
        userAddressService.save(userAddress);
        return Result.ok();
    }
    @Operation(summary = "更新用户地址")
    @PutMapping("auth/updateById")
    public Result update(@RequestBody UserAddress userAddress){
        //获取新增地址的用户id
        Long id = SpzxServiceAuthInterceptor.THREAD_LOCAL.get().getId();
        //更新地址如果为默认地址 需要更新其他的默认地址为非默认
        if(userAddress.getIsDefault()==1){
            userAddressService.update(Wrappers.lambdaUpdate(UserAddress.class)
                    .set(UserAddress::getIsDefault , 0 )
                    .eq(UserAddress::getIsDefault , 1)
                    .eq(UserAddress::getUserId , id));//更新当前用户的地址为非默认
        }
        //完整地址需要手动拼接
        //1、查询省市区 code对应的 区域名称
        Region privinceRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getProvinceCode())
                .select(Region::getName));
        Region cityRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getCityCode())
                .select(Region::getName));
        Region districtRegion = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getCode, userAddress.getDistrictCode())
                .select(Region::getName));
        String fullAddr = privinceRegion.getName()+cityRegion.getName()
                +districtRegion.getName()+" "
                + userAddress.getAddress();
        userAddress.setFullAddress(fullAddr);
        //2、拼接省市区+详细地址得到完整地址
        userAddressService.updateById(userAddress);
        return Result.ok();
    }

    @Operation(summary = "查询指定用户地址")
    @GetMapping("getUserAddress/{id}")
    public Result getUserAddress(@PathVariable("id")Long id){
        UserAddress userAddress = userAddressService.getById(id);
        return Result.ok(userAddress);
    }
    @Operation(summary = "删除指定用户地址")
    @DeleteMapping("auth/removeById/{id}")
    public Result removeById(@PathVariable("id")Long id){
        userAddressService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "查询用户地址列表")
    @GetMapping("auth/findUserAddressList")
    public Result getUsefindUserAddressListrAddress(){
        Long userId = SpzxServiceAuthInterceptor.THREAD_LOCAL.get().getId();
        List<UserAddress> list = userAddressService.list(Wrappers.lambdaQuery(UserAddress.class)
                .eq(UserAddress::getUserId, userId));
        return Result.ok(list);
    }
}
